package com.nexgo.sound;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.aop.annotation.RunOnWorkThread;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.Repository;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by xiaox on 2017/9/21.
 */

public class SoundManager {
    private static final int MAX_NUMER_STREAMS = 1;
    private static final int SOURCE_QUALITY = 0;
    private final SoundPool mSoundPool;
    private Map<Type, Integer> mSoundMap;
    private DataSource mRepository;
    private Type mSoundType;
    private int mStreamId;

    public SoundManager() {
        mSoundMap = new HashMap<>();
        mSoundPool = new SoundPool(MAX_NUMER_STREAMS, AudioManager.USE_DEFAULT_STREAM_TYPE, SOURCE_QUALITY);
        mRepository = Repository.getInstance();
    }

    public void play(Type type) {
        mSoundType = type;
        if (mSoundMap.get(type) == null) {
            int resId = 0;
            switch (type) {
                case ENTER_ID:
                    resId = R.raw.enter_id;
                    break;
                case BRING_CARD:
                    resId = R.raw.bring_card;
                    break;
                case CONFIRM_INFO:
                    resId = R.raw.confirm_info;
                    break;
                case ENTER_AMOUNT:
                    resId = R.raw.enter_amount;
                    break;
                case ENTER_PIN:
                    resId = R.raw.enter_pin;
                    break;
                case SELECT_APP:
                    resId = R.raw.select_app;
                    break;
                case SWIPE_CARD:
                    resId = R.raw.swipe_card;
                    break;
                case TRANS_FAIL:
                    resId = R.raw.trans_fail;
                    break;
                case TRANS_SUCC:
                    resId = R.raw.trans_succ;
                    break;
                default:
                    break;
            }
            int soundId = mSoundPool.load(App.sContext, resId, 1);
            mSoundPool.setOnLoadCompleteListener((soundPool, sampleId, status) -> {
                mSoundMap.put(type, soundId);
                play(soundId);
            });
        } else {
            play(mSoundMap.get(type));
        }
    }

    @RunOnWorkThread
    private void play(int loadId) {
        //判断是否开启语音提示功能
        if ("1".equals(mRepository.getParamValue(PreferencesConstants.OPEN_VOICE_PROMPT))) {
            int volume = ((AudioManager) App.sContext.getSystemService(Context.AUDIO_SERVICE)).getStreamVolume(AudioManager.STREAM_MUSIC);
            mStreamId = mSoundPool.play(loadId, volume, volume, 1, 0, 1f);
        }
    }

    public void stop() {
        if (mSoundType != null) {
            mSoundPool.stop(mStreamId);
            mSoundPool.release();
            mSoundType = null;
        }
    }

    public enum Type {
        ENTER_ID,
        BRING_CARD,
        CONFIRM_INFO,
        ENTER_AMOUNT,
        ENTER_PIN,
        SELECT_APP,
        SWIPE_CARD,
        TRANS_FAIL,
        TRANS_SUCC,
    }
}
